package com.dha.asynctask;

import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.dha.caodangcongnghe.LayDanhSachGiaoVien;
import com.dha.caodangcongnghe.R;
import com.dha.model.GiaoVienModel;
import com.dha.variable.DanhSachGiaoVienVariable;

public class DanhSachGiaoVienAsyncTask extends AsyncTask<Void, String, Void> {

	private Activity activity;
	private ProgressDialog dialog;

	private ArrayList<GiaoVienModel> arr;
	private ArrayAdapter<GiaoVienModel> adapter;

	// private ListView lvDanhSachGiaoVien;

	// public DanhSachGiaoVienAsyncTask(Activity activity) {
	// super();
	// this.activity = activity;
	// DanhSachGiaoVien.arr = new ArrayList<GiaoVienModel>();
	// adapter = new ArrayAdapter<GiaoVienModel>(activity,
	// android.R.layout.simple_list_item_1, DanhSachGiaoVien.arr);
	// lvDanhSachGiaoVien = (ListView) activity
	// .findViewById(R.id.lv_DanhSachGiaoVien);
	// lvDanhSachGiaoVien.setAdapter(adapter);
	// }

	public DanhSachGiaoVienAsyncTask(Activity activity,
			ArrayList<GiaoVienModel> arr, ArrayAdapter<GiaoVienModel> adapter) {
		super();
		this.activity = activity;
		this.arr = arr;
		this.adapter = adapter;
	}

	@Override
	protected Void doInBackground(Void... params) {
		try {
			SoapObject request = new SoapObject(
					DanhSachGiaoVienVariable.NAME_SPACE,
					DanhSachGiaoVienVariable.METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			HttpTransportSE httpTransportSE = new HttpTransportSE(
					DanhSachGiaoVienVariable.URL);
			httpTransportSE
					.call(DanhSachGiaoVienVariable.SOAP_ACTION, envelope);
			Vector<SoapObject> respone = (Vector<SoapObject>) envelope
					.getResponse();
			if (respone != null) {
				for (SoapObject so : respone) {
					publishProgress(so.getProperty("maGiaoVien").toString(), so
							.getProperty("tenGiaoVien").toString());
				}
			}
		} catch (Exception e) {
			Log.i("hehe", e.toString());
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		dialog.dismiss();
		// Toast.makeText(activity, "Chạm lâu vào 1 giáo viên để chọn",
		// Toast.LENGTH_SHORT).show();
		super.onPostExecute(result);
	}

	@Override
	protected void onPreExecute() {
		dialog = ProgressDialog.show(activity, "Vui lòng chờ",
				"Đang tải danh sách giáo viên về");
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(String... values) {
		String maGiaoVien = values[0];
		String tenGiaoVien = values[1];

		GiaoVienModel gv = new GiaoVienModel(maGiaoVien, tenGiaoVien);

		arr.add(gv);

		adapter.notifyDataSetChanged();

		super.onProgressUpdate(values);
	}

}
